package link;

import link.ListNode;

public class mergeList {
    public ListNode mergeTwoLists(ListNode head1, ListNode head2){
        ListNode pre=new ListNode(0);
        ListNode result=pre;
        while(head1!=null&&head2!=null){
            if (head1.val<head2.val){
                result.next=head1;
                head1=head1.next;
            }else{
                result.next=head2;
                head2=head2.next;
            }
            result=result.next;
        }
        if (head1!=null){
            result.next=head1;
        }
        if (head2!=null){
            result.next=head2;
        }
        return pre.next;
    }
}
